//
//  DCUpdateTool.m
//  DCProject
//
//  Created by bigbing on 2019/3/31.
//  Copyright © 2019 bigbing. All rights reserved.
//

#import "DCUpdateTool.h"
#import <UIKit/UIKit.h>

@implementation DCUpdateTool


#pragma mark - 单列
+ (DCUpdateTool *) shareClient{
    static DCUpdateTool * _instance = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _instance = [[[self class] alloc] init];
    });
    return _instance;
}


#pragma mark - 检测是否需要更新
- (BOOL)dc_isUpdateWithVersion:(NSString *)version onlineVer:(NSString *)onlineVer
{
    /* NSOrderedAscending = -1  升序
     * NSOrderedSame = 0        相等
     * NSOrderedDescending      降序
     */
    
    if (!version && !onlineVer) {
        return NO;
    }
    
    if (!version && onlineVer) {
        return YES;
    }
    
    if (version && !onlineVer) {
        return NO;
    }
    
    // 获取版本号字段
    NSArray *v1Array = [version componentsSeparatedByString:@"."];
    NSArray *v2Array = [onlineVer componentsSeparatedByString:@"."];
    // 取字段最少的，进行循环比较
    NSInteger smallCount = (v1Array.count > v2Array.count) ? v2Array.count : v1Array.count;
    
    for (int i = 0; i < smallCount; i++) {
        NSInteger value1 = [[v1Array objectAtIndex:i] integerValue];
        NSInteger value2 = [[v2Array objectAtIndex:i] integerValue];
        if (value1 > value2) {
            // v1版本字段大于v2版本字段，返回1
            return NO;
        } else if (value1 < value2) {
            // v2版本字段大于v1版本字段，返回-1
            return YES;
        }
        
        // 版本相等，继续循环。
    }
    
    // 版本可比较字段相等，则字段多的版本高于字段少的版本。
    if (v1Array.count > v2Array.count) {
        return NO;
    } else if (v1Array.count < v2Array.count) {
        return YES;
    } else {
        return NO;
    }
    
    return NO;
}


#pragma mark - 必须强制更新提示
- (void)dc_showMustUpdateTipWithUrl:(NSString *)url
{
    if (!url)  return;
    
    UIAlertController * alter = [UIAlertController alertControllerWithTitle:@"温馨提示" message:@"发现新的版本，更新后才能继续使用" preferredStyle:UIAlertControllerStyleAlert];
    [alter addAction:[UIAlertAction actionWithTitle:@"更新" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        // 打开网页
        if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:url]]) {
            if (@available(ios 10.0,*)) {
                [[UIApplication sharedApplication] openURL:[NSURL URLWithString:url] options:@{UIApplicationOpenURLOptionsSourceApplicationKey : @YES}  completionHandler:nil];
            } else {
                [[UIApplication sharedApplication] openURL:[NSURL URLWithString:url]];
            }
        }
    }]];
    [[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:alter animated:YES completion:nil];
}


#pragma mark - 非强制更新提示，可取消
- (void)dc_showUpdateTipWithUrl:(NSString *)url
{
    if (!url)  return;
    
    UIAlertController * alter = [UIAlertController alertControllerWithTitle:@"温馨提示" message:@"发现新的版本，您可以更新后使用" preferredStyle:UIAlertControllerStyleAlert];
    [alter addAction:[UIAlertAction actionWithTitle:@"更新" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:url]]) {
            if (@available(ios 10.0,*)) {
                [[UIApplication sharedApplication] openURL:[NSURL URLWithString:url] options:@{UIApplicationOpenURLOptionsSourceApplicationKey : @YES}  completionHandler:nil];
            } else {
                [[UIApplication sharedApplication] openURL:[NSURL URLWithString:url]];
            }
        }
    }]];
    [alter addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil]];
    [[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:alter animated:YES completion:nil];
}

@end
